package org.black.phoneUtility.sms.reply.appwidget;

import org.black.phoneUtility.R;
import org.black.phoneUtility.database.sqlite.PhoneUtilitySQLiteOpenHelper;
import org.black.phoneUtility.sms.reply.app.service.SmsReplyAppWidgetDeleteIntentService;
import org.black.phoneUtility.sms.reply.app.service.SmsReplyAppWidgetSwitchStatusService;
import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

public class SmsReplyAppWidgetProvider extends AppWidgetProvider {
	private static final String LOG_TAG = SmsReplyAppWidgetProvider.class
			.getName();

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Intent appWidgetDeleteIntent = new Intent(context,
				SmsReplyAppWidgetDeleteIntentService.class);
		Bundle arguments = new Bundle();
		arguments.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS,
				appWidgetIds);
		appWidgetDeleteIntent.putExtras(arguments);
		context.startService(appWidgetDeleteIntent);
		Log.d(LOG_TAG, "Start SmsReplyAppWidgetDeleteIntentService!");
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		Log.i(LOG_TAG, "Call SmsReplyAppWidgetDeleteIntentService onUpdate");
		PhoneUtilitySQLiteOpenHelper phoneUtilitySQLiteOpenHelper = new PhoneUtilitySQLiteOpenHelper(
				context);
		String queryString = "SELECT " + SmsReplyContract.SmsReplies.TITLE
				+ ", " + SmsReplyContract.SmsReplies.IS_ACTIVATED + " FROM "
				+ PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.APP_WIDGET_IDS
				+ " LEFT OUTER JOIN "
				+ PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.REPLIES
				+ " ON "
				+ PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.APP_WIDGET_IDS
				+ "."
				+ SmsReplyContract.SmsReplyAppWidgetIds.REFERENCE_SMS_REPLY_ID
				+ " = " + PhoneUtilitySQLiteOpenHelper.Tables.SMS.Reply.REPLIES
				+ "." + SmsReplyContract.SmsReplies._ID + " WHERE "
				+ SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID + " = ? ";
		for (int appWidgetId : appWidgetIds) {
			SQLiteDatabase sqLiteDatabase = null;
			Cursor cursor = null;
			try {
				sqLiteDatabase = phoneUtilitySQLiteOpenHelper
						.getReadableDatabase();
				String title = "";
				boolean isActivated = false;
				cursor = sqLiteDatabase.rawQuery(queryString, new String[] { ""
						+ appWidgetId });
				if (cursor.moveToFirst() == true) {
					title = cursor.getString(cursor
							.getColumnIndex(SmsReplyContract.SmsReplies.TITLE));
					isActivated = cursor
							.getInt(cursor
									.getColumnIndex(SmsReplyContract.SmsReplies.IS_ACTIVATED)) == 1 ? true
							: false;
				}
				SmsReplyAppWidgetProvider.updateAppWidget(context,
						appWidgetManager, 0, appWidgetId, isActivated, title);
			} catch (Throwable throwable) {
				Log.e(LOG_TAG, "Database Operation Error!", throwable);
			} finally {
				if (cursor != null) {
					try {
						cursor.close();
					} catch (Throwable th) {

					}
				}
				if (sqLiteDatabase != null) {

					try {
						sqLiteDatabase.close();
					} catch (Throwable th) {

					}
				}
			}
		}
	}

	public static void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager, long smsReplyId,
			int appWidgetId, boolean isActivated, String title) {
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.sms_reply_widget);
		remoteViews.setTextViewText(R.id.smsReplyAppWidgetTextView, title);

		Intent intent = new Intent(context,
				SmsReplyAppWidgetSwitchStatusService.class);
		intent.addCategory(String.valueOf(appWidgetId));

		Bundle bundle = new Bundle();
		bundle.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		bundle.putLong(
				SmsReplyAppWidgetSwitchStatusService.ARGUMENTS_SMS_REPLY_ID,
				smsReplyId);
		intent.putExtras(bundle);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.smsReplyAppWidgetImageButton,
				pendingIntent);
		Log.d(LOG_TAG, "Set Cliek Pending Intent! appWidgetId: " + appWidgetId);
		if (isActivated == true) {
			remoteViews.setImageViewResource(R.id.smsReplyAppWidgetImageButton,
					R.drawable.sms_reply_widget_activated_normal);
		} else {
			remoteViews.setImageViewResource(R.id.smsReplyAppWidgetImageButton,
					R.drawable.sms_reply_widget_inactivated_normal);
		}
		appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

	}
}
